package com.yyfax.pay.config.model.to;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.model.Params;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.enums.PayModeEnum;
import com.yyfax.pay.common.enums.PayRspCodeEnum;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.enums.PaymentEnum;

/**
 * 商户支付通道新增/更新参数
 * 
 * @author changxd
 * @date 2017-04-12
 * @since 1.0.0
 */
@SuppressWarnings("serial")
public class MerchantPaymentSaveParams extends Params {
	/** 商户编号 merchant_code */
	private String merchantCode;
	/** 支付通道编号 payment_code */
	private String paymentCode;
	/** 生效时间 start_time */
	private String startTime;
	/** 失效时间 end_time */
	private String endTime;
	/** 优先级 priority */
	private int priority;
	/** 支付类型 pay_type */
	private String payType;
	/** 支付模式pay_mode */
	private String payMode;
	/** 支付商户号 pay_merchid */
	private String payMerchantId;
	/** 支付扩展信息ext */
	private String ext;
	/** 状态 state */
	private String state;
	/** 状态 remark */
	private String remark;
	/** 创建时间 createTime */
	private String createTime;
	
	

	@Override
	public boolean validate() throws Exception {
		if (StringUtils.isBlank(merchantCode)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'merchantCode'不能为空");
		}
		if (StringUtils.isBlank(paymentCode)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'paymentCode'不能为空");
		}
		if (StringUtils.isBlank(startTime)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'startTime'不能为空");
		}
		if (StringUtils.isBlank(endTime)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'end_time'不能为空");
		}
		if (priority < 0) {
			throw new YYException(PayRspCodeEnum.PAY_CODE_PRIPORITY_ERROR, "参数 'priority'必须大于等于0");
		}
		if (StringUtils.isBlank(payType)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'payType'不能为空");
		}
		if (StringUtils.isBlank(payMode)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'payMode'不能为空");
		}
		if (StringUtils.isBlank(payMerchantId)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'payMerchantId'不能为空");
		}
		if (StringUtils.isBlank(ext)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'ext'不能为空");
		}
		if (StringUtils.isBlank(state)) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "参数 'state'不能为空");
		}
		//校验参数是否在枚举中
		validateEnum(paymentCode,payMode,payType);
		return true;
	}
	
	public void validateEnum(String paymentCode, String payMode, String payType) throws Exception {
		boolean codeFlag = false;
		boolean modeFlag = false;
		boolean typeFlag = false;
		List<PaymentEnum> codelist = PaymentEnum.values();
		List<PayModeEnum> modelist = PayModeEnum.values();
		List<PayTypeEnum> typelist = PayTypeEnum.values();
		
		for (int i = 0; i < codelist.size(); i++) {
			JSONObject result = JSONObject.parseObject(codelist.get(i).toString());
			String value=(String) result.get("value");
			if (paymentCode.equals(value)) {
				codeFlag = true;
			}
		}
		if (!codeFlag) {
			throw new YYException(PayRspCodeEnum.PAY_CODE_PAYMENTCODE_ERROR, "参数paymentCode不在枚举类型中");
		}
		for (int i = 0; i < modelist.size(); i++) {
			JSONObject result = JSONObject.parseObject(modelist.get(i).toString());
			String value=(String) result.get("value");
			if (payMode.equals(value)) {
				modeFlag = true;
			}
		}
		if (!modeFlag) {
			throw new YYException(PayRspCodeEnum.PAY_CODE_PAYMODE_ERROR, "参数payMode不在枚举类型中");
		}
		for (int i = 0; i < typelist.size(); i++) {
			JSONObject result = JSONObject.parseObject(typelist.get(i).toString());
			String value=(String) result.get("value");
			if (payType.equals(value)) {
				typeFlag = true;
			}
		}
		if (!typeFlag) {
			throw new YYException(PayRspCodeEnum.PAY_CODE_PAYTYPE_ERROR, "参数payType不在枚举类型中");
		}
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getPaymentCode() {
		return paymentCode;
	}

	public void setPaymentCode(String paymentCode) {
		this.paymentCode = paymentCode;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getPayType() {
		return payType;
	}

	public void setPayType(String payType) {
		this.payType = payType;
	}

	public String getPayMode() {
		return payMode;
	}

	public void setPayMode(String payMode) {
		this.payMode = payMode;
	}

	public String getPayMerchantId() {
		return payMerchantId;
	}

	public void setPayMerchantId(String payMerchantId) {
		this.payMerchantId = payMerchantId;
	}

	public String getExt() {
		return ext;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
}
